import {
	getToken,
	setToken
} from '@/utils/token.js'
import {
	queryBrowerQty,
	insertComBrowsingHistory,
	insertBrowsingHistory
} from '@/common/api/common.js'

import store from '@/store/index.js'
const state = {
	theLoginStatus: getToken('useNum') ? true : false,
	isFresh: false,
	isLogin: getToken('isLogin') || false,
	isOpen: false,
	cardData: {},
	editHtml:'',
}
const getters = {
	isFresh: state => state.isFresh,
	theLoginStatus: state => state.theLoginStatus,
	cardData: state => state.cardData,
	isOpen: state => state.isOpen,
	editHtml: state => state.editHtml,
}
const mutations = {
	setEditHtml(state,data){
		//console.log('setEditHtml',data)
		state.editHtml = data
	},
	//去用户详情
	toUserIndex(state, data, type = 1) {
		let acPath = ''
		switch (data.moduleNo) {
			case 'ydylapp1001': //企业基础模板
				acPath =
					`/pages/index/cardIndex/detailIndex?isshare=1&compId=${data.companyId}&version=2&moduleNo=${data.moduleNo}`
				break;
			case 'ydylapp1002': //企业模板一
				acPath = `/pages/index/cardIndex/detailIndex?compId=${data.companyId}&moduleNo=${data.moduleNo}`
				break;
			case 'ydylapp1003': //企业模板二
				acPath = `/pages/index/cardIndex/detailIndex?compId=${data.companyId}&moduleNo=${data.moduleNo}`
				break;
			case 'ydylapp2001': //个人基础模板
				acPath =
					`/pages/index/cardIndex/detailIndex?isshare=1&useId=${data.craftsmanId}&version=1&moduleNo=${data.moduleNo}`
				break;
			case 'ydylapp2002': //个人模板一
				acPath = `/pages/index/cardIndex/detailIndex?useId=${data.craftsmanId}&moduleNo=${data.moduleNo}`
				break;
			case 'ydylapp2003': //个人模板二（
				acPath = `/pages/index/cardIndex/detailIndex?useId=${data.craftsmanId}&moduleNo=${data.moduleNo}`
				break;
			default:
				acPath = ''
				break
		}
		if (acPath) {
			uni.navigateTo({
				url: acPath
			})
		}
		return acPath
	},
	toWindowUrl() {
		if (getToken('windowUrl')) {
			// #ifdef H5
			let winUrl = getToken('windowUrl')
			setToken('windowUrl', '')
			window.location.replace(winUrl)
			// #endif
		}
	},
	isFresh(state, data) {
		state.isFresh = data
	},
	setIsLogin(state, data) {
		state.isLogin = data
		setToken('isLogin', data)
	},
	toLogin(state) {
		// var ua = window.navigator.userAgent.toLowerCase();
		// if (ua.match(/MicroMessenger/i) == 'micromessenger') {
			// 微信浏览器打开
			// store.dispatch('showAuthModal');
			// window.location.href =
			// 	`https://job.ydylmold.cn/sysWxInfor/wxlogin/${getToken('assoId')?(getToken('modleName') || 'jixieqiyequan'):'mingpian'}`
		// } else {
			// store.dispach('showAuthModal');
		// }
		store.dispatch('showAuthModal');
	},
	//企业身份登录
	setCompLogin(state, data) { //设置企业登录
		setToken('complogin', data.usrCompany)
		setToken('version', 2)
		state.version = data.companyNo
		setToken('useNum', String(data.companyNo))
	},
	toIndex(state) { //跳转到首页
		let jumpUrlCard = getToken('indexTab') || '/pages/index/index'
		if (getToken('jumpUrlCard')) {
			jumpUrlCard = getToken('jumpUrlCard')
			setToken('jumpUrlCard', '')
			// #ifdef H5
			window.location.replace(jumpUrlCard)
			// #endif
			// #ifdef MP-WEIXIN
			uni.navigateTo({
				url: jumpUrlCard
			})
			// #endif
		} else {
			uni.switchTab({
				url: jumpUrlCard
			})
		}
	}
}
const actions = {
	
	//获取查看数量
	async queryBrowerQty({
		commit,
		state
	}, sendData) {
		if (sendData.userId || sendData.compId) {
			let params = {}
			if (sendData.userId) {
				params['craftsmanId'] = sendData.userId
			} else if (sendData.compId) {
				params['companyId'] = sendData.compId
			}
			queryBrowerQty(params).then(res => {
				if (res.data && res.data.code) {
					state.cardData = res.data.data
				} else {
					state.cardData = {}
					uni.showToast({
						title: '请求出错',
						icon: 'none'
					})
				}
			})
		}
	},
	async isToOther({
		commit,
		dispatch,
		state
	}, sendData) {
		let params = {}
		if (sendData.version == 1) { //访问他人个人主页
			params['loginUserId'] = getToken('useNum')
			params['craftsmanId'] = sendData.userId
			params['viewType'] = getToken('version')
			let res1 = await insertBrowsingHistory(params)
			if (res1.data && res1.data.code) {
				dispatch('queryBrowerQty', {
					userId: sendData.userId
				})
			}
		} else { //访问他人企业主页
			params['craftsmanId'] = getToken('useNum')
			params['companyNo'] = sendData.userId
			params['viewType'] = getToken('version')
			let res2 = await insertComBrowsingHistory(params)
			if (res2.data && res2.data.code) {
				dispatch('queryBrowerQty', {
					compId: sendData.userId
				})
			}
		}
	},
}
export default {
	state,
	mutations,
	actions,
	getters
}
